package xyz.syyrjx.blog.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import xyz.syyrjx.blog._enum.State;
import xyz.syyrjx.blog.entity.Blog;
import xyz.syyrjx.blog.entity.Result;
import xyz.syyrjx.blog.entity.Types;
import xyz.syyrjx.blog.service.BlogService;
import xyz.syyrjx.blog.service.CommentService;
import xyz.syyrjx.blog.service.TypeService;
import xyz.syyrjx.blog.util.ContentUtil;
import xyz.syyrjx.blog.util.PageUtil;
import xyz.syyrjx.blog.util.StringUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Resource
    private TypeService typeServer;
    @Resource
    private BlogService blogServer;
    @Resource
    private CommentService commentService;

    @GetMapping("/listBlogInPageByType")
    @ResponseBody
    public Result listBlogInPageByType(Integer page,Integer next
            ,String search, Long typeId){
        search = StringUtil.searchDeal(search);
        Blog template = new Blog();
        template.setType(typeId);
        template.setTitle(search);
        template.setState(State.NORMAL_STATE.getCode());
        int count = blogServer.countBlogs(search,typeId);
        page = PageUtil.pageGet(count,page,next,5);
        List<Blog> blogList = blogServer.queryBlogs(template,page);
        for(Blog blog : blogList){
            blog.setTypes(typeServer.queryTypeById(blog.getType()));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("blogList",blogList);
        map.put("page",page);
        return Result.success(map);
    }

    @GetMapping("/blog")
    public ModelAndView blog(String search,Long typeId,Integer page,Integer next){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/blog");
        return mv;
    }

    @GetMapping("/publish")
    public ModelAndView publish(){
        ModelAndView mv = new ModelAndView();
        List<Types> typesList = typeServer.queryAllTypes();
        mv.addObject("typeList",typesList);
        mv.setViewName("admin/publish");
        return mv;
    }

    @PostMapping("/pushBlog")
    @ResponseBody
    public Result push(Blog blog){
        try {
            String title = blog.getTitle();
            String picture = "images/blogPic/" + title + "/" + blog.getPicture();
            blog.setPicture(picture);
            blogServer.addBlog(blog);
            return Result.success("发布成功");
        }catch (Exception e) {
            return Result.fail(e.toString());
        }
    }

    @RequestMapping("/blogDelete")
    @ResponseBody
    public Result blogDelete(long id, int page, String search, Long typeId){
        commentService.deleteCommentInBlog(id);
        blogServer.deleteBlogById(id);
        int count = blogServer.countBlogs(search,typeId);
        double temp = count *1.0/5;
        int countPage = (int)temp;
        if(temp >(int)temp){
            countPage += 1;
        }
        if (page > countPage) {
            page = countPage;
        }
        search = StringUtil.searchDeal(search);
        Blog template = new Blog();
        template.setType(typeId);
        template.setTitle(search);
        template.setState(State.NORMAL_STATE.getCode());
        List<Blog> blogList = blogServer.queryBlogs(template,page);
        for(Blog blog : blogList){
            blog.setTypes(typeServer.queryTypeById(blog.getType()));
        }
        int finalPage = page;
        Map<String, Object> map = new HashMap<String, Object>(){{
            put("blogList",blogList);
            put("page", finalPage);
        }};
        return Result.success(map);
    }

    @GetMapping("/blogModify")
    public ModelAndView blogModify(Long id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/blogModify");
        return mv;
    }

    @GetMapping("/getBlogById")
    @ResponseBody
    public Result getBlogById(Long id){
        Blog template = new Blog();
        template.setId(id);
        Blog blog = blogServer.queryBlogs(template,null).get(0);
        blog.setTypes(typeServer.queryTypeById(blog.getType()));
        String picturePath = blog.getPicture();
        String title = blog.getTitle();
        blog.setPicture(picturePath.substring(16 + title.length()));
        return Result.success(blog);
    }

    @PostMapping("/modifyBlog")
    @ResponseBody
    public Result modifyBlog(Blog blog){
        try {
            blog.setUpdateDate(new Date());
            String title = blog.getTitle();
            String picture = "images/blogPic/" + title + "/" + blog.getPicture();
            blog.setPicture(picture);
            blogServer.updateBlog(blog);
            return Result.success("修改成功");
        }catch (Exception e) {
            return Result.fail("修改失败" + e);
        }
    }

}
